const path = require('path');//引入path模块
function resolve(dir) {
    return path.join(__dirname, dir)//path.join(__dirname)设置绝对路径
}
const CompressionPlugin = require('compression-webpack-plugin')


module.exports = {
    devServer: {
        disableHostCheck: true,
        proxy: {
            '/api': { //请求路径关键字
                target: 'http://localhost:3000/', //对应自己的接口
                changeOrigin: true,//是否允许跨域,在本地会创建一个虚拟服务端，然后发送请求的数据，
                // 并同时接收请求的数据，这样服务端和服务端进行数据的交互就不会有跨域问题
                ws: true,
                pathRewrite: {
                    '^/api': ''      //这里理解成用‘/api’代替target里面的地址，后面组件中我们掉接口时直接用api代替
                    // 比如我要调用'http://localhost:8024/management/user/add'，直接写‘/api/user/add’即可
                }
            },
            '/sockjs-node': {
                target: 'http://127.0.0.1:3000',
                ws: false,
                changeOrigin: true
            },
            "/api2": {
                target: "http://c.3g.163.com/nc/article/list/T1348648517839/0-20.html",
                ws: true,
                pathRewrite: {
                    '^/api2': ""
                }
            }
        },
    },
    lintOnSave: false,
    chainWebpack: (config) => {
        config.resolve.alias
            //set第一个参数：设置的别名，第二个参数：设置的路径
            .set('@', resolve('./src'))
            .set('components', resolve('./src/components'))
            .set('assets', resolve('./src/assets'))
            .set('views', resolve('./src/views'))
            .set('network', resolve('./src/network'))
        //注意 store 和 router 没必要配置
    },
    configureWebpack: () => {
        if (process.env.NODE_ENV === 'production') {
            return {
                plugins: [new CompressionPlugin({
                    test: /\.js$|\.html|\.css/,
                    threshold: 10240,
                    deleteOriginalAssets: false,
                    minRatio: 0.8
                })]
            }
        } else {
            return {
                module: {
                    rules: [ // fixes https://github.com/graphql/graphql-js/issues/1272
                        {
                            test: /\.mjs$/,
                            include: /node_modules/,
                            type: 'javascript/auto'
                        }
                    ]
                }
            }
        }
    }
}